<template>
  <div
    class="wrap-item"
    :style="{
      '--scroll-width': '175px',
    }"
  >
    <!-- 搜索-->
    <el-form ref="formSearch" :model="formSearch" size="small" inline>
      <el-form-item>
        <el-input
          v-model="formSearch.param.orderName"
          maxlength="20"
          placeholder="请输入工单名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="formSearch.param.title"
          maxlength="50"
          placeholder="请输入工单标题"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="formSearch.param.status"
          placeholder="请选择状态"
          clearable
        >
          <el-option label="待接单" :value="0"></el-option>
          <el-option label="已接单" :value="1"></el-option>
          <el-option label="已处理" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="formSearch.param.date"
          @change="dateChange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始创建时间"
          end-placeholder="结束创建时间"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <span class="wrap-btn wrap-primary" @click="search">查询</span>
      </el-form-item>
    </el-form>
    <!-- 数据列表 -->
    <div class="wrap-other-roll">
      <el-table
        :data="tableData"
        :header-cell-style="{ background: '#F6F7F9' }"
        row-key="id"
        default-expand-all
        :tree-props="{ children: 'childList', hasChildren: 'hasChildren' }"
      >
        <el-table-column
          type="index"
          width="50"
          label="序号"
          align="center"
        ></el-table-column>
        <el-table-column prop="orderName" label="工单名称"></el-table-column>
        <el-table-column prop="title" label="工单标题">
          <template slot-scope="scope">
            <el-tooltip
              popper-class="ellipsis-tip"
              :content="scope.row.title"
              placement="top-start"
            >
              <span class="ellipsis">{{ scope.row.title }}</span>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column prop="typeName" label="工单类型"></el-table-column>
        <el-table-column prop="expireDataObj" label="处理时限">
          <template slot-scope="scope">
            <span v-if="scope.row.expireDataObj.type == 0"
              >{{ scope.row.expireDataObj.expire }}天</span
            >
            <span v-if="scope.row.expireDataObj.type == 1"
              >{{ scope.row.expireDataObj.expire }}小时</span
            >
          </template>
        </el-table-column>
        <el-table-column prop="submitUserName" label="联系人"></el-table-column>
        <el-table-column
          prop="submitUserPhone"
          label="联系电话"
        ></el-table-column>
        <el-table-column prop="expireDataObj" label="状态">
          <template slot-scope="scope">
            <span v-if="scope.row.status == 0">待接单</span>
            <span v-if="scope.row.status == 1 || scope.row.status == -1"
              >已接单</span
            >
            <span v-if="scope.row.status == 2">待接单</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="appointTime"
          label="指派时间"
          width="150px"
        ></el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          width="150px"
        ></el-table-column>
        <el-table-column
          prop="content"
          label="操作"
          width="80px"
          align="center"
        >
          <template slot-scope="scope">
            <el-dropdown @command="(command) => handle(command, scope.row)">
              <span class="el-dropdown-link">
                更多
                <i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="view" v-if="$checkPermission(104)"
                  >查看</el-dropdown-item
                >
                <el-dropdown-item
                  command="take"
                  v-if="scope.row.status == 0 && $checkPermission(116)"
                  >接单</el-dropdown-item
                >
                <el-dropdown-item
                  command="reject"
                  v-if="scope.row.status == 0 && $checkPermission(118)"
                  >驳回</el-dropdown-item
                >
                <el-dropdown-item
                  command="dispose"
                  v-if="scope.row.status == 1 && $checkPermission(119)"
                  >处理回复</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <web-page
      :page="formSearch.pageNum"
      :size="formSearch.pageSize"
      :total="total"
      @sizeChange="sizeChange"
      @currentChange="currentChange"
    ></web-page>
    <!-- 查看 -->
    <detail-item :visible="isLook" :data="lookData" @close="isLook = false" />
    <!-- 审批信息 -->
    <el-drawer
      title="审批信息"
      :wrapperClosable="false"
      :visible.sync="isReject"
    >
      <el-form label-width="80px" size="medium">
        <el-form-item label="驳回原因" required>
          <el-input
            type="textarea"
            v-model="rejectReason"
            maxlength="200"
            placeholder="请输入驳回原因"
            show-word-limit
            rows="4"
            resize="none"
          ></el-input>
        </el-form-item>
        <el-form-item style="text-align: end">
          <span class="wrap-btn wrap-success" @click="saveReject">确定</span>
          <span class="wrap-btn wrap-default" @click="visible = false">取消</span>
        </el-form-item>
      </el-form>
    </el-drawer>
    <!-- 处理回复 -->
    <el-drawer
      title="工单处理"
      :wrapperClosable="false"
      :visible.sync="isDispose"
      size="666px"
    >
      <el-form label-width="80px" :model="form" size="medium">
        <el-form-item label="是否解决" required>
          <el-select v-model="form.isSolved" style="width: 100%">
            <el-option label="是" value="0"></el-option>
            <el-option label="否" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="问题原因">
          <el-input
            type="textarea"
            v-model="form.reason"
            maxlength="100"
            placeholder="请输入驳回原因"
            rows="2"
            resize="none"
          ></el-input>
        </el-form-item>
        <el-form-item label="费用明细">
          <el-row
            class="post-row"
            v-for="(item, index) in form.costVOList"
            :key="'post' + index"
            :gutter="10"
          >
            <el-col :span="8">
              <el-input
                size="small"
                v-model="item.costName"
                maxlength="50"
                placeholder="费用名称"
                clearable
              ></el-input>
            </el-col>
            <el-col :span="6">
              <el-input-number
                v-model="item.quantity"
                controls-position="right"
                :min="1"
                :max="10000000"
                size="small"
                placeholder="数量"
              ></el-input-number>
            </el-col>
            <el-col :span="6">
              <el-input-number
                v-model="item.price"
                controls-position="right"
                :min="1"
                :max="1000000000"
                size="small"
                placeholder="单价"
              ></el-input-number>
            </el-col>
            <el-col :span="2" :offset="1">
              <el-button size="small" type="danger" @click="removePost(item)"
                >删除</el-button
              >
            </el-col>
          </el-row>
          <el-row class="post-row" :gutter="10">
            <el-col :span="24" style="text-align: right"
              ><el-button size="small" type="success" @click="addPost"
                >新增</el-button
              ></el-col
            ></el-row
          >
        </el-form-item>
        <el-form-item label="附件">
          <web-upload @upload="upload" type="file" />
          <el-table :data="form.fileList" v-if="form.fileList.length > 0">
            <el-table-column prop="name" label="名称">
              <template slot-scope="scope">
                <el-tooltip
                  popper-class="ellipsis-tip"
                  :content="scope.row.name"
                  placement="top-start"
                >
                  <span class="ellipsis">{{ scope.row.name }}</span>
                </el-tooltip>
              </template></el-table-column
            >
            <el-table-column prop="size" label="大小"></el-table-column>
            <el-table-column
              prop="content"
              label="操作"
              width="100px"
              align="center"
            >
              <template slot-scope="scope">
                <el-link
                  type="primary"
                  :underline="false"
                  @click="downloadFile(scope.row)"
                  >下载</el-link
                >
                <el-link
                  type="danger"
                  :underline="false"
                  style="margin-left: 10px"
                  @click="remove(scope.row)"
                  >删除</el-link
                >
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            type="textarea"
            v-model="form.remarks"
            maxlength="100"
            placeholder="请输入备注"
            rows="2"
            resize="none"
          ></el-input>
        </el-form-item>
        <el-form-item style="text-align: end">
          <span class="wrap-btn wrap-success" @click="saveDispose">确定</span>
          <span class="wrap-btn wrap-default" @click="isDispose = false">取消</span>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import detailItem from './../components/detail'
export default {
  components: { detailItem },
  data() {
    return {
      formSearch: {
        pageNum: 1,
        pageSize: 10,
        param: {},
      },
      total: null,
      tableData: [],
      rowId: '',
      isReject: false,
      rejectReason: '',
      isDispose: false,
      form: { isSolved: '0', costVOList: [], fileList: [] },
      lookData: {},
      isLook: false,
    }
  },
  created() {
    this.itemList()
  },
  methods: {
    itemList() {
      // 获取列表
      this.$axios
        .post(
          '/workorder/myToDoAppoint/myToOrderReceivingPage',
          this.formSearch
        )
        .then((data) => {
          this.tableData = []
          if (data.code == 200) {
            this.tableData = data.data.list
            this.total = data.data.total
          } else {
            this.$message.error(data.msg)
          }
        })
    },
    sizeChange(val) {
      //每页条数
      this.formSearch.pageSize = val
      this.itemList()
    },
    currentChange(val) {
      //分页
      this.formSearch.pageNum = val
      this.itemList()
    },
    search() {
      //搜索
      this.formSearch.pageNum = 1
      this.itemList()
    },
    handle(command, row) {
      //操作
      if (!command) {
        this.$message.error('菜单选项缺少command属性')
        return
      }
      switch (command) {
        case 'view':
          //查看
          this.$axios
            .get('/workorder/workOrder/workOrderProcessDetail?id=' + row.id)
            .then((data) => {
              this.lookData = {}
              if (data.code == 200) {
                this.lookData = data.data
                this.isLook = true
              } else {
                this.$message.error(data.msg)
              }
            })
          break
        case 'take':
          //接单
          this.$axios
            .get(
              '/workorder/myToDoApprove/myToDoOrderTakingHandle?id=' + row.id
            )
            .then((res) => {
              if (res.code == 200) {
                this.$message.success('操作成功！')
                this.itemList()
              } else {
                this.$message.error(res.msg)
              }
            })
          break
        case 'reject':
          //驳回
          this.rejectReason = ''
          this.rowId = row.id
          this.isReject = true
          break
        case 'dispose':
          //处理回复
          this.form = {
            id: row.id,
            workOrderId: row.id,
            isSolved: '0',
            costVOList: [],
            fileList: [],
          }
          this.isDispose = true
          break
      }
    },
    saveReject() {
      //保存审批信息
      if (!this.rejectReason) {
        this.$message.error('请输入驳回原因')
        return
      }
      this.$axios
        .post('/workorder/myToDoApprove/myToDoOrderTakingReject', {
          id: this.rowId,
          rejectReason: this.rejectReason,
        })
        .then((data) => {
          if (data.code == 200) {
            this.isReject = false
            this.itemList()
            this.$message.success('操作成功！')
          } else {
            this.$message.error(data.msg)
          }
        })
    },
    saveDispose() {
      //保存处理回复
      this.form.annex = JSON.stringify(this.form.fileList)
      this.$axios
        .post('workorder/myToDoApprove/myToDoOrderTakingReply', this.form)
        .then((data) => {
          if (data.code == 200) {
            this.isDispose = false
            this.itemList()
            this.$message.success('操作成功！')
          } else {
            this.$message.error(data.msg)
          }
        })
    },
    upload(data) {
      //上传
      this.form.fileList.push(data)
    },
    addPost() {
      //新增费明细
      this.form.costVOList.push({
        costName: '',
        quantity: 0,
        price: 0,
      })
    },
    removePost(item) {
      //删除费明细
      var index = this.form.costVOList.indexOf(item)
      if (index !== -1) {
        this.form.costVOList.splice(index, 1)
      }
    },
    dateChange(val) {
      if (val) {
        this.formSearch.param.startTime = val[0]
        this.formSearch.param.endTime = val[1]
      } else {
        this.formSearch.param.startTime = ''
        this.formSearch.param.endTime = ''
      }
    },
    downloadFile(item) {
      //下载附件
      const a = document.createElement('a')
      a.download = item.name
      a.href = item.fileDomain + item.url
      a.click()
    },
    remove(item) {
      //删除
      var index = this.form.fileList.indexOf(item)
      if (index !== -1) {
        this.form.fileList.splice(index, 1)
      }
    },
  },
}
</script>
<style lang="scss" scoped>
.post-row {
  margin: 0 !important;
}
</style>
